$.fn.extend({
    // 初始化棋盘的个数
    initGameBoard(boardX,boardY,EveryGridSize,isborder = true){
        const ganmeBoardDom = this.eq(0); //游戏板的原生dom
        
        for(let y = 0; y < boardY;y++){
            const divX = document.createElement('div');
            for(let x = 0; x < boardX;x++){
                const grid = document.createElement('div');
                let isBorderStyle = {};
                if(isborder){
                    isBorderStyle = {
                        borderLeft:'1px solid black',
                        borderTop:'1px solid black',
                        borderRight:x == boardX - 1 ? '1px solid black': '',
                        borderBottom:y == boardY - 1 ? '1px solid black': ''
                    }
                }
                $(grid).addClass(isborder == true ? '' : 'grid')
                .css({
                    display:'inline-block',
                    width:EveryGridSize + 'px',
                    height: EveryGridSize + 'px',
                    boxSizing:'border-box',
                    ...isBorderStyle
                })
                .data({
                    i:y,
                    j:x
                })
                .appendTo(divX)
            }
            $(divX).css({
                fontSize:'0px'
            })
            .appendTo(ganmeBoardDom)
        }
        return ganmeBoardDom
    },
    //插入颜色格子东西到grid中  color:1代表黑色棋子 color：2代表白色棋子
    appendToGrid(color,EveryGridSize,opacity = 1){
        $(this).css('position','relative');
        const span = document.createElement('span');
        // 透明度不足1认为是落点位置
        if(opacity < 1){
            $(span).attr('id','confirmChess')
        }
        $(span).addClass('chess')
        .css({
            position:'absolute',
            boxSizing:'border-box',
            border:'1px solid black',
            width: EveryGridSize / 2 + 'px',
            height: EveryGridSize / 2 + 'px',
            borderRadius:'50%',
            top:'50%',
            left:'50%',
            transform:'translate(-50%,-50%)',
            backgroundColor: color == 2 ? 'white':'black',
            opacity
        })        
        .appendTo($(this))
        return this;
    }
})


// 计算棋盘的宽高
$.extend({
    calBooardSize(width,height){
        return {
            width : width + 'px',
            height : height + 'px',
        }
    },
    initBoardData(arr,x,y){
        for(let i = 0;i < y;i++){
            const temp = new Array(x).fill(0)
            arr.push(temp)
        }
        return arr;
    },
    isGameOver(boardData,i,j,color,callback){
        //四个方向是否存在五个相同棋子
        let x = j, y = i;
        let sameLen = boardData[y][x] == color ? 1 : 0;

        //水平
        while(x > 0&& boardData[y][--x] == color) sameLen++;
        x = j;
        while(x < boardData[0].length - 1 && boardData[y][++x] == color) sameLen++;
        if(sameLen >= 5) {
            callback(true)
            return;
        }   

        //垂直
        x = j;
        sameLen = boardData[y][x] == color ? 1 : 0;
        while(y > 0&& boardData[--y][x] == color) sameLen++;
        y = i;
        while(y < boardData.length - 1 && boardData[++y][x] == color) sameLen++;
        if(sameLen >= 5) {
            callback(true)
            return;
        }   


        // \方向
        x = j;
        y = i;
        sameLen = boardData[y][x] == color ? 1 : 0;
        while(y > 0&& x > 0&& boardData[--y][--x] == color) sameLen++;
        x = j;
        y = i;
        while(x <  boardData[0].length - 1 && y < boardData.length - 1 && boardData[++y][++x] == color) sameLen++;
        if(sameLen >= 5) {
            callback(true)
            return;
        }   

        // /方向
        x = j;
        y = i;
        sameLen = boardData[y][x] == color ? 1 : 0;
        while(y < boardData.length - 1 && x > 0&& boardData[++y][--x] == color) sameLen++;
        x = j;
        y = i;
        while(y > 0 && x < boardData.length - 1 && boardData[--y][++x] == color) sameLen++;
        if(sameLen >= 5) {
            callback(true)
            return;
        }     

        callback(false)
    }
})